package com.winhxd.b2c.order.controller;

import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.order.condition.OrderRebateRecordCondition;
import com.winhxd.b2c.common.domain.order.model.OrderInfo;
import com.winhxd.b2c.common.domain.order.model.OrderRebateRecord;
import com.winhxd.b2c.common.domain.order.vo.OrderInfoVO;
import com.winhxd.b2c.common.domain.order.vo.OrderRebateRecordVO;
import com.winhxd.b2c.common.exception.BusinessException;
import com.winhxd.b2c.common.feign.order.OrderServiceClient;
import com.winhxd.b2c.common.util.JsonUtil;
import com.winhxd.b2c.order.service.OrderService;
import io.swagger.annotations.*;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(tags = "OrderService")
public class OrderServiceController implements OrderServiceClient {

    private static final Logger logger = LoggerFactory.getLogger(OrderServiceController.class);

    @Autowired
    private OrderService orderService;

    @Override
    public ResponseResult<List<OrderRebateRecordVO>> orderClosedCalculateRevenue(@RequestParam("orderNo") String orderNo) {
        ResponseResult<List<OrderRebateRecordVO>> result = new ResponseResult<>();
        try {
            OrderInfo orderInfo = orderService.selectByOrderNo(orderNo);
            List<OrderRebateRecordVO> orderRebateRecordVOList = orderService.orderCalculateRevenue(orderInfo, false);
            result.setData(orderRebateRecordVOList);
        }catch (Exception e){
            result.setCode(BusinessCode.CODE_1001);
            result.setMessage("OrderServiceController->orderClosedCalculateRevenue异常:" + e.getMessage());
            logger.error("{}订单转正收益计算异常:{}", "#收益计算# - ", e);
        }
        return result;
    }

    @Override
    public ResponseResult<OrderInfoVO> getOrderInfoByOrderNo(@RequestParam("orderNo") String orderNo) {
        ResponseResult<OrderInfoVO> result = new ResponseResult<>();
        try {
            OrderInfoVO orderInfo = orderService.selectInfoByOrderNo(orderNo);
            result.setData(orderInfo);
        }catch (Exception e){
            result.setCode(BusinessCode.CODE_1001);
            result.setMessage("OrderServiceController->getOrderInfoByOrderNoe异常:" + e.getMessage());
            logger.error("查询订单VO异常:{}", e);
        }
        return result;
    }

    @Override
    public ResponseResult<List<OrderRebateRecord>> getOrderRebateRecordVO(@Param("orderNo") String orderNo) {
        ResponseResult<List<OrderRebateRecord>> result = new ResponseResult<>();
        try {
            List<OrderRebateRecord> orderRebateRecordVOList = orderService.getOrderRebateRecordVO(orderNo);
            result.setData(orderRebateRecordVOList);
        }catch (Exception e){
            result.setCode(BusinessCode.CODE_1001);
            result.setMessage("OrderServiceController->getOrderRebateRecordVO异常:" + e.getMessage());
            logger.error("查询订单奖励VO异常:{}", e);
        }
        return result;
    }

    @Override
    public ResponseResult<OrderInfo> modifyOrderRebateRecord(OrderRebateRecordCondition condition) {
        String logTitle = "/api-order/order/4005/v1/modifyOrderRebateRecord-修改实际收益";

        if(null == condition){
            logger.error(logTitle+"--参数为空！");
            throw new BusinessException(BusinessCode.CODE_403006);
        }
        logger.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
        ResponseResult<OrderInfo> result = new ResponseResult<>();

        result.setData(orderService.modifyOrderRebateRecord(condition));

        logger.info("{}=--结束 result={}", logTitle, result);
        return result;
    }
}
